package com.zzyl.nursing.service.impl;

import cn.hutool.core.lang.UUID;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.*;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.HuaWeiIoTService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 华为IoTService接口
 *
 * @author 怡宝
 */
@Service
@Slf4j
public class HuaWeiIoTServiceImpl implements HuaWeiIoTService {
    @Autowired
    private IoTDAClient client;

    @Override
    public List<ProductSummary> selectProduceList() {
        ListProductsRequest listProductsRequest = new ListProductsRequest();
        listProductsRequest.setLimit(50);
        ListProductsResponse response = client.listProducts(listProductsRequest);
        if (response.getHttpStatusCode() != 200) {
            throw new BaseException("物联网接口 - 查询产品，同步失败");
        }
        List<ProductSummary> products = response.getProducts();
        return products;
    }

    /**
     * 注册设备
     *
     * @param dto
     */
    @Override
    public AddDeviceResponse addDevice(DeviceDto dto) {
        AddDeviceRequest request = new AddDeviceRequest();
        AddDevice body = new AddDevice();
        body.setDeviceName(dto.getDeviceName());
        body.setNodeId(dto.getNodeId());
        body.setProductId(dto.getProductKey());
        AuthInfo authInfo = new AuthInfo();
        String secret = UUID.fastUUID().toString(true);
        authInfo.setSecret(secret);
        body.setAuthInfo(authInfo);
        request.setBody(body);
        try {
            AddDeviceResponse response = client.addDevice(request);
            return response;
        } catch (Exception e) {
            log.error("注册设备失败", e);
            throw new ServiceException("注册设备失败！请稍候重试");
        }
    }

    /**
     * @param iotId
     */
    @Override
    public ShowDeviceResponse selectDeviceByIotId(String iotId) {
        ShowDeviceRequest request = new ShowDeviceRequest();
        request.setDeviceId(iotId);
        try {
            ShowDeviceResponse response = client.showDevice(request);
            return response;
        } catch (Exception e) {
            log.error("查询云平台上的设备数据失败", e);
            throw new ServiceException("查询云平台上的设备数据失败");
        }
    }

    /**
     * 查询设备影子数据
     *
     * @param iotId
     */
    @Override
    public ShowDeviceShadowResponse queryServiceProperties(String iotId) {
        ShowDeviceShadowRequest request = new ShowDeviceShadowRequest();
        request.setDeviceId(iotId);
        try {
            ShowDeviceShadowResponse response = client.showDeviceShadow(request);
            return response;
        } catch (Exception e) {
            log.error("查询设备最新数据失败", e);
            throw new ServiceException("查询设备最新数据失败");
        }
    }

    /**
     * 修改设备
     *
     * @param dto
     */
    @Override
    public void updateDeviceByIotId(DeviceDto dto) {
        UpdateDeviceRequest request = new UpdateDeviceRequest();
        request.setDeviceId(dto.getIotId());
        UpdateDevice body = new UpdateDevice();
        AuthInfoWithoutSecret authInfoBody = new AuthInfoWithoutSecret();
        authInfoBody.setSecureAccess(true);
        body.setAuthInfo(authInfoBody);
        body.setDeviceName(dto.getDeviceName());
        body.setDescription(dto.getRemark());
        request.setBody(body);
        try {
            UpdateDeviceResponse response = client.updateDevice(request);
        } catch (Exception e) {
            log.error("修改设备失败", e);
            throw new ServiceException("修改设备失败");
        }
    }

    /**
     * 删除设备
     *
     * @param iotId
     */
    @Override
    public void deleteDeviceByIotId(String iotId) {
        DeleteDeviceRequest request = new DeleteDeviceRequest();
        request.setDeviceId(iotId);
        try {
            DeleteDeviceResponse response = client.deleteDevice(request);
        } catch (Exception e) {
            log.error("删除设备失败", e);
            throw new ServiceException("删除设备失败");
        }
    }
}
